package com.such.kit.database.support;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import com.such.kit.bean.BeanCommons;
import com.such.kit.database.dialect.TypeFactory;
import com.such.kit.placeholder.PlaceholderCommons;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

public abstract class DDLBean<T> implements Serializable {

	private static final long serialVersionUID = -1603133029129992873L;

	public abstract void converter(T databaseType);

	public abstract Map<String, String> buildRendererParams(Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping, Map<String, String> params, TypeFactory type);

	protected void putString(String attributeName, String attributeValue, Map<String, String> params, Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping) {
		if (ValidateCommons.isNotEmpty(attributeValue)) {
			attributeName = buildPlaceholderKey(attributeName, getClass());
			String resourceKey = resourceKeyMapping.get(attributeName);
			params.put(attributeName, attributeValue);
			if (ValidateCommons.isNotEmpty(resourceKey)) {
				params.put(resourceKey, ddlRenderer(ddlTemplateMapping, resourceKey, params));
			}
		}
	}

	protected void putBoolean(String attributeName, boolean attributeValue, Map<String, String> params, Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping) {
		if (attributeValue) {
			attributeName = buildPlaceholderKey(attributeName, getClass());
			String resourceKey = resourceKeyMapping.get(attributeName);
			params.put(attributeName, String.valueOf(attributeValue));
			if (ValidateCommons.isNotEmpty(resourceKey)) {
				params.put(resourceKey, ddlRenderer(ddlTemplateMapping, resourceKey, params));
			}
		}
	}

	protected void putList(String attributeName, List<String> attributeValues, Map<String, String> params, Map<String, String> resourceKeyMapping, Map<String, String> ddlTemplateMapping) {
		if (ValidateCommons.isNotEmpty(attributeValues)) {
			attributeName = buildPlaceholderKey(attributeName, getClass());
			StringBuffer valueBuf = new StringBuffer();
			for (String value : attributeValues) {
				if (valueBuf.length() > 0) {
					valueBuf.append(", ");
				}
				valueBuf.append(value);
			}

			String resourceKey = resourceKeyMapping.get(attributeName);
			params.put(attributeName, valueBuf.toString());
			if (ValidateCommons.isNotEmpty(resourceKey)) {
				params.put(resourceKey, ddlRenderer(ddlTemplateMapping, resourceKey, params));
			}
		}
	}

	protected String ddlRenderer(Map<String, String> ddlTemplateMapping, String resourceKey, Map<String, String> params) {
		String ddlTemplate = ddlTemplateMapping.get(resourceKey);
		return PlaceholderCommons.replacePlaceholder(ddlTemplate, params);
	}

	public static String buildPlaceholderKey(String attributeName, Class<?> clazz) {
		return StringCommons.merger(BeanCommons.getSimpleName(clazz), ".", attributeName);
	}

}
